﻿//-----------------------------------------------------------------------
// <copyright>
// Copyright (C) Sergey Solyanik for The Black Square Project.
//
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Xml.Linq;

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace DocumentManager
{
    /// <summary>
    /// Locks and unlocks Word document for editing. Comments can still be added to a locked document.
    /// </summary>
    public static class DocumentLocker
    {
        /// <summary>
        /// Locks/unlocks the file.
        /// </summary>
        /// <param name="file"> File name. </param>
        /// <param name="isLock"> True if lock, false if unlock. </param>
        private static void LockUnlock(string file, bool isLock)
        {
            WordprocessingDocument doc = WordprocessingDocument.Open(file, true);
            doc.ExtendedFilePropertiesPart.Properties.DocumentSecurity =
                new DocumentFormat.OpenXml.ExtendedProperties.DocumentSecurity(isLock ? "8" : "0");
            doc.ExtendedFilePropertiesPart.Properties.Save();

            DocumentProtection dp =
                doc.MainDocumentPart.DocumentSettingsPart.Settings.ChildElements.First<DocumentProtection>();
            if (dp != null)
            {
                dp.Remove();
            }

            if (isLock)
            {
                dp = new DocumentProtection();
                dp.Edit = DocumentProtectionValues.Comments;
                dp.Enforcement = new DocumentFormat.OpenXml.OnOffValue(true); // "1"
                doc.MainDocumentPart.DocumentSettingsPart.Settings.AppendChild(dp);
            }

            doc.MainDocumentPart.DocumentSettingsPart.Settings.Save();

            doc.Close();
        }

        /// <summary>
        /// Unlocks the file.
        /// </summary>
        /// <param name="file"> File name. </param>
        public static void Unlock(string file)
        {
            LockUnlock(file, false);
        }

        /// <summary>
        /// Locks the file. Only comments can be added to a locked file.
        /// </summary>
        /// <param name="file"> File name. </param>
        public static void Lock(string file)
        {
            LockUnlock(file, true);
        }

        /// <summary>
        /// Returns true if the file is locked.
        /// </summary>
        /// <param name="file"> The file name. </param>
        /// <returns> True if the file is locked. </returns>
        public static bool IsLocked(string file)
        {
            WordprocessingDocument doc = WordprocessingDocument.Open(file, true);
            bool isLocked = doc.ExtendedFilePropertiesPart.Properties.DocumentSecurity.Text.Equals("8");
            doc.Close();
            return isLocked;
        }
    }
}
